package com.hy.demo.app;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.gyf.immersionbar.ImmersionBar;
import com.hy.demo.R;
import com.hy.frame.common.ILoadingDialog;
import com.hy.frame.common.ILoadingUI;
import com.hy.frame.ui.LoadingDialog;
import com.hy.frame.ui.LoadingUI;
import com.hy.frame.ui.ToolbarUI;
import com.hy.frame.util.LogUtil;

/**
 * title 无
 * author heyan
 * time 19-7-11 下午2:30
 * desc 无
 */
public abstract class BaseActivity extends com.hy.frame.base.BaseActivity {
    private ToolbarUI mToolbarUI;
    private ILoadingDialog mLoadingDialog;
    private ILoadingUI mLoadingUI;

    @Override
    public int getScreenOrientation() {
        return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }

    protected boolean isSingleLayout() {
        return false;
    }

    protected int getLayoutId() {
        return 0;
    }

    public boolean isTranslucentStatus() {
        return true;
    }

    @Override
    protected boolean initAttrs() {
        return true;
    }

    protected boolean isConfigBack() {
        return true;
    }

    @Override
    protected void initLayout() {
        if (getLayoutId() == 0) return;
        if (isSingleLayout()) {
            setContentView(getLayoutId());
        } else {
            setContentView(R.layout.v_base);
            FrameLayout cMain = findViewById(R.id.kf_main);
            if (cMain != null)
                View.inflate(this, getLayoutId(), cMain);
        }
        Toolbar toolbar = findViewById(R.id.kf_toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            mToolbarUI = new ToolbarUI(this, new Runnable() {
                @Override
                public void run() {
                    onBackPressed();
                }
            }, new Runnable() {
                @Override
                public void run() {
                    onRightClick();
                }
            }).setTranslucentStatus(isTranslucentStatus()).build((ViewGroup) toolbar.getParent());
            if (isConfigBack()) {
                mToolbarUI.setLeftIcon(R.mipmap.ic_back);
            }
            mToolbarUI.setListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onViewClick(v);
                }
            });
            if (isTranslucentStatus()){
                ImmersionBar.with(this).statusBarColor(R.color.theme).navigationBarColorInt(Color.WHITE).init();
            }
        }
        getLifecycle().addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                LogUtil.d(BaseActivity.this.getClass(), "event=" + event);
            }
        });
    }

    public ToolbarUI getToolbarUI() {
        return this.mToolbarUI;
    }

    public ILoadingUI getLoadingUI() {
        if (mLoadingUI == null) {
            FrameLayout cMain = findViewById(R.id.kf_main);
            if (cMain != null) {
                mLoadingUI = new LoadingUI(cMain);
                mLoadingUI.setErrorClick(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onLoadViewClick();
                    }
                });
            }
        }
        return mLoadingUI;
    }

    public ILoadingDialog getLoadingDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(getContext());
        }
        return mLoadingDialog;
    }

    @Override
    public void setTitle(CharSequence title) {
        if (getToolbarUI() != null)
            getToolbarUI().setTitle(title);
        else
            super.setTitle(title);
    }

    protected void onRightClick() {

    }

    protected void onLoadViewClick() {

    }
    protected void onViewClick(View v) {

    }

}
